package com.fanfq.loancalc;

import java.util.Calendar;



import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;

public class Calc5Activity extends Activity {
	
	private EditText mCalc_5_4;
	private EditText mCalc_5_5;
	private TextView mCalc_5_14;
	private EditText mCalc_5_15;
	
	private Spinner mSpinner_0;
	private Spinner mSpinner_1;
	
	// date and time
    private int mYear;
    private int mMonth;
    private int mDay;
    private RadioGroup mRadioGroup_1;
    
    static final int DATE_DIALOG_CALC_5_4 = 1;
    static final int DATE_DIALOG_CALC_5_5 = 2;
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calc_5);
        
        mSpinner_0 = (Spinner) findViewById(R.id.spinner_0);
        ArrayAdapter<CharSequence> adapter_0 = ArrayAdapter.createFromResource(
                this, R.array.calc0, android.R.layout.simple_spinner_item);
        adapter_0.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mSpinner_0.setAdapter(adapter_0);
        
        mSpinner_1 = (Spinner) findViewById(R.id.spinner_1);
        ArrayAdapter<CharSequence> adapter_1 = ArrayAdapter.createFromResource(
                this, R.array.calc8, android.R.layout.simple_spinner_item);
        adapter_1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mSpinner_1.setAdapter(adapter_1);
        
        mCalc_5_4 = (EditText)findViewById(R.id.calc_5_4);
        mCalc_5_4.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				showDialog(DATE_DIALOG_CALC_5_4);
				return false;
			}
        });
        
        mCalc_5_5 = (EditText)findViewById(R.id.calc_5_5);
        mCalc_5_5.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				showDialog(DATE_DIALOG_CALC_5_5);
				return false;
			}
        });
        
        mCalc_5_14 = (TextView)findViewById(R.id.calc_5_14);
        mCalc_5_15 = (EditText)findViewById(R.id.calc_5_15);
        
        mRadioGroup_1 = (RadioGroup) findViewById(R.id.group_1);
        mRadioGroup_1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if(R.id.calc_5_10 == checkedId){
					mCalc_5_14.setVisibility(View.VISIBLE);
					mCalc_5_15.setVisibility(View.VISIBLE);
				}else{
					mCalc_5_14.setVisibility(View.GONE);
					mCalc_5_15.setVisibility(View.GONE);
				}
			}
		});
        
        
        
        
        
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);

        updateCalc_5_4();
        updateCalc_5_5();
    }
	
	@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DATE_DIALOG_CALC_5_4:
                return new DatePickerDialog(this,
                		mDateSetListenerCalc_5_4,
                            mYear, mMonth, mDay);
            case DATE_DIALOG_CALC_5_5:
                return new DatePickerDialog(this,
                		mDateSetListenerCalc_5_5,
                            mYear, mMonth, mDay);
        }
        return null;
    }
	
	@Override
    protected void onPrepareDialog(int id, Dialog dialog) {
		switch (id) {
        case DATE_DIALOG_CALC_5_4:
            ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
            break;
        case DATE_DIALOG_CALC_5_5:
            ((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
            break;
		}
    }    

    private void updateCalc_5_4() {
    	mCalc_5_4.setText(
            new StringBuilder()
                    // Month is 0 based so add 1
			    	.append(mYear).append("-")
			        .append(mMonth + 1).append("-")
			        .append(mDay));
    }
    
    private void updateCalc_5_5() {
    	mCalc_5_5.setText(
            new StringBuilder()
                    // Month is 0 based so add 1
			    	.append(mYear).append("-")
			        .append(mMonth + 1).append("-")
			        .append(mDay));
    }

    private DatePickerDialog.OnDateSetListener mDateSetListenerCalc_5_4 =
            new DatePickerDialog.OnDateSetListener() {

                public void onDateSet(DatePicker view, int year, int monthOfYear,
                        int dayOfMonth) {
                    mYear = year;
                    mMonth = monthOfYear;
                    mDay = dayOfMonth;
                    updateCalc_5_4();
                }
            };
    private DatePickerDialog.OnDateSetListener mDateSetListenerCalc_5_5 =
        new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                mYear = year;
                mMonth = monthOfYear;
                mDay = dayOfMonth;
                updateCalc_5_5();
            }
        };

}
